/*
 * UserModel.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import android.graphics.Bitmap;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.constant.Preferences;
import com.yidiandao.business.constant.ServerCode;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.User;
import com.yidiandao.business.data.event.RefreshDataEvent;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.UploadFileRequest;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.ChangeNickNameRequest;
import com.yidiandao.business.data.request.ChangePasswordRequest;
import com.yidiandao.business.data.request.ChangeSexRequest;
import com.yidiandao.business.data.request.CheckMobileRequest;
import com.yidiandao.business.data.request.CheckShopNameRequest;
import com.yidiandao.business.data.request.CommitInformationRequest;
import com.yidiandao.business.data.request.GetAuthCodeRequest;
import com.yidiandao.business.data.request.LoginRequest;
import com.yidiandao.business.data.request.LogoutRequest;
import com.yidiandao.business.data.request.QueryUserInitInformationRequest;
import com.yidiandao.business.data.request.RegisterRequest;
import com.yidiandao.business.data.request.ResetPasswordRequest;
import com.yidiandao.business.data.response.LoginResponse;
import com.yidiandao.business.data.response.PersonalInfoResponse;
import com.yidiandao.business.data.response.QueryUserInitInformationResponse;
import com.yidiandao.business.data.response.SignInfoResponse;
import com.yidiandao.business.data.response.UploadLicenseResponse;
import com.yidiandao.business.utils.BuglyUtils;
import com.yidiandao.business.utils.FileUtils;
import com.yidiandao.business.utils.PictureUtils;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.PushUtils;
import com.yidiandao.business.utils.SSLUtils;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

/**
 * UserModel具体实现.
 */
public class UserModel implements IUserModel {

    private NetBiz mNetBiz = new NetBiz();

    @Override
    public void postRefreshGoodsPriceEvent() {
        EventBus.getDefault().post(new RefreshDataEvent());
    }

    @Override
    public void getAuthCode(String phoneNumber, String tag, final OnAccessNetListener listener) {

        final GetAuthCodeRequest request = new GetAuthCodeRequest();
        request.setUrl(Urls.GET_AUTH_CODE);
        request.setTag(tag);
        request.setMobile(phoneNumber);

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void login(
            final String phoneNumber,
            String password,
            String deviceId,
            final String tag,
            final OnLoginListener listener) {

        final LoginRequest request = new LoginRequest();
        request.setUrl(Urls.LOGIN);
        request.setMobile(phoneNumber);
        request.setPassword(SSLUtils.getEncryptPassword(password));
        request.setDeviceId(deviceId);
        request.setTag(tag);

        mNetBiz.sendPostRequest(request, LoginResponse.class, new NetListener<LoginResponse>() {
            @Override
            public void onSuccess(LoginResponse response) {
                LoginResponse.Data data = response.getData();
                //当前选择的城市与注册的城市不一致时  强制切换城市
                String signCity = data.getCity();
                // 先保存Token 以免判断登录状态出现bug
                PreferenceUtils.setToken(UIUtils.getContext(), data.getToken());

                // 设置登录用户 --> 方便 Preferences 非 common 模式的值的存取
                PreferenceUtils.setAccount(UIUtils.getContext(), phoneNumber);

                // 极光推送设置别名
                PushUtils.setAlias(UIUtils.getContext(), data.getUserId());

                // 保存注册的城市
                PreferenceUtils.setString(UIUtils.getContext(), Preferences.SIGN_CITY, data.getCity(), false);

                // 将user_id存储.区分不同的用户。读取数据库时使用
                PreferenceUtils.setUserId(UIUtils.getContext(), data.getUserId());

                // 是否是代理商
                int isAgent = data.getIsAgent();
                PreferenceUtils.setBoolean(UIUtils.getContext(), Preferences.IS_AGENT, isAgent == LoginResponse.USER_AGNET, false);

                boolean needChangeAddress = false;
                postRefreshGoodsPriceEvent();

                if (listener != null) {
                    listener.onSuccess(needChangeAddress, signCity);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (bizErrorInfo.getCode()
                        == ServerCode.Register.UN_WRITE_INFORMATION_AND_UN_PASS) {
                    // 未填写审核资料,未通过审核
                    // 可能是通过审核之后登录进入App,然后Token被保存, 然后再未通过审核
                    clearUserInfo(false);
                    if (listener != null) {
                        listener.onUnWriteInformation();
                    }
                } else if (bizErrorInfo.getCode() == ServerCode.Register.UN_REGISTER) {
                    // 可能是通过审核之后登录进入App,然后Token被保存, 然后再未注册
                    clearUserInfo(false);
                } else {
                    if (listener != null) {
                        listener.onFailed();
                    }
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void register(String shopName, String phoneNumber, String authCode, String password,
                         String invitationCode, String tag, final OnAccessNetListener listener) {

        RegisterRequest request = new RegisterRequest();
        request.setUrl(Urls.REGISTER);
        request.setTag(tag);
        request.setMobile(phoneNumber);
        request.setAuthCode(authCode);
        request.setShopName(shopName);
        request.setPassword(SSLUtils.getEncryptPassword(password));
        request.setInvitationCode(invitationCode);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void logout(String deviceId, final boolean postEvent, String tag,
                       final OnAccessNetListener listener) {

        LogoutRequest request = new LogoutRequest();
        request.setUrl(Urls.LOGOUT);
        request.setTag(tag);
        request.setDeviceId(deviceId);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {

                clearUserInfo(postEvent);

                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void clearUserInfo(boolean postEvent) {
        // 重置token为默认值
        PreferenceUtils.setToken(UIUtils.getContext(), PreferenceUtils.STRING_DEFAULT);

        // 重置推送的alias
        PushUtils.clearAlias(UIUtils.getContext());

        // 重置我是代理商标记
        PreferenceUtils.setBoolean(UIUtils.getContext(), Preferences.IS_AGENT, false, false);

        //重置user_id
        PreferenceUtils.setUserId(UIUtils.getContext(), PreferenceUtils.STRING_DEFAULT);
    }

    @Override
    public boolean isLogin() {
        return !PreferenceUtils.STRING_DEFAULT.equals(
                PreferenceUtils.getToken(UIUtils.getContext()));
    }

    @Override
    public File getTakePhotoTempFile() {
        return new File(PictureUtils.getAlbumDir(), Common.TEMP_TAKE_PHOTO);
    }

    @Override
    public File getPhotoAlbumsTempFile() {
        return new File(PictureUtils.getAlbumDir(), Common.TEMP_PHOTO_ALBUMS);
    }

    @Override
    public void resetPassword(String phoneNumber, String newPassword, String authCode, String tag,
                              final OnAccessNetListener listener) {

        ResetPasswordRequest request = new ResetPasswordRequest();
        request.setUrl(Urls.RESET_PASSWORD);
        request.setTag(tag);
        request.setMobile(phoneNumber);
        request.setAuthCode(authCode);
        request.setPassword(SSLUtils.getEncryptPassword(newPassword));

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void uploadShopLicense(File file, String tag, final OnUploadLicenseListener listener) {

        UploadFileRequest request = new UploadFileRequest();
        request.setUrl(Urls.UPLOAD_LICENSE);
        request.setTag(tag);
        request.addUploadFile("picture", file);

        mNetBiz.uploadFiles(request, UploadLicenseResponse.class,
                new NetListener<UploadLicenseResponse>() {
                    @Override
                    public void onSuccess(UploadLicenseResponse response) {
                        if (listener != null) {
                            UploadLicenseResponse.Data data = response.getData();
                            listener.onSuccess(data.getUploadPic(), data.getPicture());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void uploadAvatar(File file, String tag, final OnUploadLicenseListener listener) {

        UploadFileRequest request = new UploadFileRequest();
        request.setUrl(Urls.CHANGE_HEADER_IMG);
        request.setTag(tag);
        request.addUploadFile("picture", file);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.uploadFiles(request, UploadLicenseResponse.class,
                new NetListener<UploadLicenseResponse>() {
                    @Override
                    public void onSuccess(UploadLicenseResponse response) {
                        if (listener != null) {
                            listener.onSuccess(null, response.getData().getUploadPic());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void commitInformation(
            String typeId, String userId, String realName,
            int provinceId, int cityId, int areaId,
            String address, String uploadImageUrl, String tag, final OnAccessNetListener listener) {

        CommitInformationRequest request = new CommitInformationRequest();
        request.setUrl(Urls.COMMIT_INFORMATION);
        request.setTag(tag);
        request.setUserId(userId);
        request.setReadName(realName);
        request.setProvinceId(provinceId);
        request.setCityId(cityId);
        request.setAreaId(areaId);
        request.setAddress(address);
        request.setTypeId(typeId);
        request.setUploadPicUrl(uploadImageUrl);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void initInformation(
            String mobile, String tag, final OnInitInformationListener listener) {

        QueryUserInitInformationRequest request = new QueryUserInitInformationRequest();
        request.setUrl(Urls.COMPLETE_USER_INFORMATION);
        request.setMobile(mobile);
        request.setTag(tag);

        mNetBiz.sendGetRequest(request, QueryUserInitInformationResponse.class,
                new NetListener<QueryUserInitInformationResponse>() {
                    @Override
                    public void onSuccess(QueryUserInitInformationResponse response) {
                        if (listener != null) {
                            listener.onSuccess(response.getData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void getUserInfo(String tag, final OnGetPersonalInfoListener listener) {
        BaseRequest request = new BaseRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.USER_INFO);
        mNetBiz.sendGetRequest(request, PersonalInfoResponse.class,
                new NetListener<PersonalInfoResponse>() {
                    @Override
                    public void onSuccess(PersonalInfoResponse personalInfoResponse) {
                        User data = personalInfoResponse.getData();
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void getPersonalInfo(String tag, final OnGetPersonalInfoListener listener) {
        BaseRequest request = new BaseRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.MEMBER_HOME);

        mNetBiz.sendGetRequest(request, PersonalInfoResponse.class,
                new NetListener<PersonalInfoResponse>() {
                    @Override
                    public void onSuccess(PersonalInfoResponse personalInfoResponse) {
                        User data = personalInfoResponse.getData();
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void setNickName(String nickName, final OnAccessNetListener listener, String tag) {

        ChangeNickNameRequest request = new ChangeNickNameRequest();
        request.setUrl(Urls.CHANGE_USER_INFO);
        request.setTag(tag);
        request.setName(nickName);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendPostRequest(request, BaseResponse.class,
                new NetListener<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse baseResponse) {
                        if (listener != null) {
                            listener.onSuccess();
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void setSex(String tag, String sex, final OnAccessNetListener listener) {
        ChangeSexRequest request = new ChangeSexRequest();
        request.setUrl(Urls.CHANGE_USER_INFO);
        request.setTag(tag);
        request.setSex(sex);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void changePassword(
            String oldPassword,
            String newPassword,
            String tag,
            final OnAccessNetListener listener) {

        ChangePasswordRequest request = new ChangePasswordRequest();
        request.setUrl(Urls.CHANGE_PASSWORD);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setOldPassword(SSLUtils.getEncryptPassword(oldPassword));
        request.setNewPassword(SSLUtils.getEncryptPassword(newPassword));

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void setHasNewAgentPushMessage(boolean hasNewAgentMessage) {
        PreferenceUtils.setBoolean(UIUtils.getContext(), Preferences.NEW_PUSH_AGENT_MESSAGE,
                hasNewAgentMessage, false);
    }

    @Override
    public boolean hasNewAgentPushMessage() {
        return PreferenceUtils.getBoolean(UIUtils.getContext(), Preferences.NEW_PUSH_AGENT_MESSAGE,
                false);
    }

    @Override
    public void setHasNewOfferMessage(boolean hasNewOfferMessage) {
        PreferenceUtils.setBoolean(UIUtils.getContext(), Preferences.NEW_PUSH_OFFER_MESSAGE,
                hasNewOfferMessage, false);
    }

    @Override
    public boolean hasNewOfferMessage() {
        return PreferenceUtils.getBoolean(UIUtils.getContext(), Preferences.NEW_PUSH_OFFER_MESSAGE,
                false);
    }

    @Override
    public void changePushSwitch(boolean isChecked) {
        PreferenceUtils.setBoolean(
                UIUtils.getContext(), Preferences.JPUSH_SWITCH, isChecked, false);
        if (isChecked) {
            PushUtils.resumePush(UIUtils.getContext());
        } else {
            PushUtils.stopPush(UIUtils.getContext());
        }
    }

    @Override
    public boolean isPushSwitchOpen() {
        return PreferenceUtils.getBoolean(
                UIUtils.getContext(), Preferences.JPUSH_SWITCH, true, false);
    }

    @Override
    public void checkShopNameExist(String shopName, String tag) {
        CheckShopNameRequest request = new CheckShopNameRequest();
        request.setTag(tag);
        request.setUrl(Urls.CHECK_SHOP_NAME);
        request.setShopName(shopName);

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void checkMobile(String phoneNumber, String tag, final OnCheckListener listener) {

        CheckMobileRequest request = new CheckMobileRequest();
        request.setTag(tag);
        request.setUrl(Urls.CHECK_MOBILE);
        request.setMobile(phoneNumber);

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    return listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    return listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public File pressImage(String path) {
        //压缩图片
        Bitmap bm = PictureUtils.getSmallBitmap(path, 480, 800);
        //保存图片
        return FileUtils.saveBitmapFile(bm, "/upload_" + System.currentTimeMillis() + ".jpg");
    }

    @Override
    public void checkIsSign(String tag, final OnCheckSignListener listener) {
        //判断是否打开提示
        boolean isOpen = PreferenceUtils.getBoolean(UIUtils.getContext(),
                Preferences.IS_OPEN_SIGN_TIP, true, false);
        //是否已登录
        boolean isLogin = !PreferenceUtils.getToken(UIUtils.getContext()).equals(
                PreferenceUtils.STRING_DEFAULT);
        if (!(isLogin && isOpen)) {
            return;
        }
        final BaseRequest request = new BaseRequest();
        request.setUrl(Urls.CHECK_IS_SIGN_IN);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        mNetBiz.sendGetRequest(request, SignInfoResponse.class,
                new NetListener<SignInfoResponse>() {
                    @Override
                    public void onSuccess(SignInfoResponse response) {
                        SignInfoResponse.DataBean data = response.getData();
                        boolean visiable = false; //是否展示签到提示
                        if (null != data) {
                            PreferenceUtils.setString(UIUtils.getContext(), Intents.SIGN_URL, data.getSignUrl(), false);
                            int type = data.getType(); //0未签到  1已签到  2活动未开始
                            //获取今日时间
                            final String time = data.getToday();
                            boolean isSameDay =
                                    PreferenceUtils.getString(UIUtils.getContext(), Preferences.DAY_TIME, PreferenceUtils.STRING_DEFAULT, false).equals(time);
                            if (type == 0) {
                                visiable = true;
                                saveSignTime(time, isSameDay);
                            } else {
                                visiable = false;
                            }
                        }
                        if (null != listener) {
                            listener.onSuccess(visiable);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return false;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    private void saveSignTime(String time, boolean isSameDay) {
        if (!isSameDay) {
            //记录提示签到时间
            PreferenceUtils.setString(UIUtils.getContext(), Preferences.DAY_TIME, time, false);
        }
    }

    @Override
    public String getLocatingCity() {
        return PreferenceUtils.getString(UIUtils.getContext(), Preferences.CITY, "", true);
    }

    @Override
    public void setUserIdForBugly() {
        BuglyUtils.setUserId(PreferenceUtils.getAccount(UIUtils.getContext()));
    }
}
